package com.java.demo.demo;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther zhangwj
 * @date 2021/4/20 上午11:53
 */
public class Demo5 {

    public static Map<String, Object> map = new HashMap<>();

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        Test test = new Test();
        map.put("test", test);
        List<Object> params = new ArrayList<>();

        List<String> name = new ArrayList<>();
        name.add("1111111");
        params.add(name);
        String s = getResult("test", "get", params, String.class);
        System.out.println(s);
    }

    public static <T> T getResult(String key, String methodName, List<Object> params, Class<T> tClass) throws InvocationTargetException, IllegalAccessException {
        Object object = map.get(key);
        Method[] methods = object.getClass().getMethods();
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                Object o = method.invoke(object, params.toArray());
                return tClass.cast(o);
            }
        }
        return null;
    }
}

class Test {

    public String get(List<String> name) {
        System.out.println(name.size());
        return "名称为" + name.get(0);
    }

}
